<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" ref="serchInput" clearable v-model="queryData.query" @clear="goodList">
            <el-button slot="append" @click="searchGoods" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="16">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
    <!-- 表格区域 -->
    <el-table :data="goodList" style="width: 100%" border stripe>
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="goods_name" label="商品名称" width="650"></el-table-column>
      <el-table-column prop="goods_price" label="价格" width="70"></el-table-column>
      <el-table-column prop="goods_number" label="数量" width="70"></el-table-column>
      <el-table-column prop="add_time" label="添加时间" width="200">
        <template slot-scope="scope">
          {{ scope.row.add_time | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
        <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
        <el-button type="danger" size="mini" icon="el-icon-delete" @click="delGoods(scope.row.goods_id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码条区域 -->
    <el-pagination @size-change="handleSizeChange" :page-size="5" @current-change="pageChange"
      :current-page="queryData.pagenum" :page-sizes="[1, 3, 5, 10]"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 分类查询参数
      queryData: {
        query: '', // 查询关键字
        pagenum: 1, // 当前页码
        pagesize: 5 // 当前页容量
      },
      // 商品列表数据
      goodList: [],
      // 总商品数量
      total: 0,
      // 编辑分类规则
      EditCateRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 2, max: 7, message: '长度在 2 到 7 个字符', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 点击跳转页面到添加商品页面
    addGoods () {
      this.$router.push('/goods/Add')
    },
    // 删除商品
    delGoods (id) {
      this.$messageBox.confirm('确认删除?', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        callback: async (action) => {
          // 如果点击取消按钮，则停止执行函数
          if (action === 'cancel') return
          // 发起请求 删除分类
          const { data: res } = await this.$http.delete(`goods/${id}`)
          // 如果删除失败，弹出错误
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
          // 如果成功，渲染页面
          this.$message.success(res.meta.msg)
          this.getGoodsList()
        }
      })
    },
    // 查询商品
    searchGoods () {
      // 判断关键字是否为空
      if (this.queryData.query.trim().length === 0) {
        // 让文本框获取焦点
        this.$refs.serchInput.focus()
        return this.$message.error('请输入搜索关键字')
      }
      this.getGoodsList()
    },
    // 页容量改变事件
    handleSizeChange (newSize) {
      // 拿到最新的页容量
      this.queryData.pagesize = newSize
      this.getGoodsList()
    },
    // 监听当前页码的改变事件
    pageChange (newPage) {
      this.queryData.pagenum = newPage
      this.getGoodsList()
    },
    // 获取商品分类
    async getGoodsList () {
      // 发起请求，获取商品列表
      const { data: res } = await this.$http.get('goods', {
        params: this.queryData
      })
      // 如果请求失败
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 如果请求成功
      this.goodList = res.data.goods
      this.total = res.data.total
    }
  }
}
</script>

<style lang="less" scoped>
.el-table{
  margin-top: 15px;
}
</style>
